package com.hna.face.controller.sys;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.hna.face.base.BaseController;
import com.hna.face.model.BfRole;
import com.hna.face.model.BfRole2menu;
import com.hna.face.model.BfUser;
import com.hna.face.model.BfUser2role;
import com.hna.face.service.IBfRole2menuService;
import com.hna.face.service.IBfRoleService;
import com.hna.face.service.IBfUser2roleService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 角色管理
 * @author zhulei
 *
 */
@Controller
public class RoleController extends BaseController{	
	
	private final static Logger log = LoggerFactory.getLogger(RoleController.class);

	
	@Autowired
	private IBfRoleService roleService;
	
	@Autowired
	IBfUser2roleService user2RoleService ; 
	
	@Autowired
	IBfRole2menuService role2MenuService;
	
	@RequestMapping("/role/index")
	public String index(HttpServletRequest request) {
		return "pages/role/index";
	}
	
	@RequestMapping("/role/add")
	public String add(HttpServletRequest request) {
		return "pages/role/add";
	}
	

	@RequestMapping("/role/edit")
	public ModelAndView edit(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView("pages/role/edit");
		modelAndView.addObject("id", request.getParameter("id"));
		return modelAndView;
	}
	
	
	/**
	 * 角色管理
	 * 
	 * @param
	 * @return
	 * @throws
	 */
	@RequestMapping(value = { "/rest/roleList" }, method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> roleList(HttpServletRequest request,BfRole role) {
		Map<String,Object> res = new HashMap<String,Object>();
		  
		try {
			BfUser user=(BfUser)request.getSession().getAttribute("userInfo");			
			
			Wrapper<BfRole> wrapper=new EntityWrapper<BfRole>();
			
			List<BfRole> roleList=roleService.selectList(wrapper);
			
			res.put("total", roleList.size());
			res.put("rows", roleList);
			res.put("success", "true");
			return res;
		} catch (Exception e) {
			res.put("success", "false");
			return res;
		} 	
		
	}
	
	/**
	 * 角色添加
	 * 
	 * @param
	 * @return
	 * @throws
	 */
	@RequestMapping(value = { "/rest/roleAdd" }, method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> roleAdd(HttpServletRequest request,BfRole role) {
		
		Map<String,Object> res = new HashMap<String,Object>();
		try {	
			
//			if(hasRole(role.getRolename())){
//			  res.put("success", "false");
//	          res.put("msg", "角色已存在！");
//	          return res;
//			}
			BfUser userSession =(BfUser)request.getSession().getAttribute("userInfo");
			roleService.insert(role);
			this.saveUserlog(request, "角色管理："+userSession.getUsername()+"添加角色操作");
			res.put("success", "true");
			return res;
		} catch (Exception e) {
			res.put("success", "false");
			res.put("msg", e.getMessage());
			return res;
		}
		
	}
//	private boolean hasRole(String roleName){
//	  RoleExample example = new RoleExample();
//	  example.createCriteria().andRolenameEqualTo(roleName);
//	  if(CollectionUtils.isNotEmpty(roleService.selectByExample(example))){
//	    return true;
//	  }
//	  return false;
//	}
	/**
	 * 角色信息详情
	 * 
	 * @param
	 * @return
	 * @throws
	 */
	@RequestMapping(value = { "/rest/roleInfo" }, method = RequestMethod.GET)
	public @ResponseBody
	BfRole roleInfo(HttpServletRequest request) {
		BfRole role=null;
		try {	
			Long id=Long.parseLong( request.getParameter("id"));
			role=roleService.selectById(id);
			return role;
		} catch (Exception e) {
			return role;
		}
		
	}
	
	/**
	 * 角色修改
	 * 
	 * @param
	 * @return
	 * @throws
	 */
	@RequestMapping(value = { "/rest/roleUpdate" }, method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> roleUpdate(HttpServletRequest request,BfRole role) {
		
		Map<String, Object> res = new HashMap<String,Object>();
		try {			
			BfUser userSession =(BfUser)request.getSession().getAttribute("userInfo");
//			ParkRole role= new ParkRole();
//			BeanConvertUtils.frxConvertByDateTime(role, request.getParameterMap());
//			if(!request.getParameter("oldRolename").equals(role.getRolename())&&hasRole(role.getRolename())){
//              res.put("success", "false");
//              res.put("msg", "角色已存在！");
//              return res;
//            }
			roleService.updateById(role);
			this.saveUserlog(request, "角色管理："+userSession.getUsername()+"修改角色操作");
			res.put("success", "true");
			return res;
		} catch (Exception e) {
			res.put("success", "false");
			res.put("msg", e.getMessage());
			return res;
		}
		
	}
	
	
	/**
	 * restful
	 * 角色删除
	 * @param req
	 * @return

	 */
	@RequestMapping(value = { "/rest/roleDel" }, method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> roleDel(HttpServletRequest request){
		Map<String, Object> res = new HashMap<String,Object>();
		try {
			BfUser userSession =(BfUser)request.getSession().getAttribute("userInfo");
			//RoleExample roleExample = new RoleExample();
			//BeanUtils.populate(roleExample, request.getParameterMap());
			 String abc=request.getParameter("ids");
			String[] ids = abc.split(",");
			List<Long> longIds = new ArrayList<Long>(); 
			for(String id : ids){
				longIds.add(Long.valueOf(id));
			}
			
//			User2RoleExample user2RoleExample = new User2RoleExample();
//			user2RoleExample.createCriteria().andRoleidIn(longIds);
//			List<ParkUser2role> urList = user2RoleService.selectByExample(user2RoleExample);
			Wrapper<BfUser2role> wrapper=new EntityWrapper<BfUser2role>();
			wrapper.in("roleid", longIds);
			List<BfUser2role> urList = user2RoleService.selectList(wrapper);
			

			
			if(!urList.isEmpty()||urList.size()>0){
				res.put("success", "false");
				res.put("msg","角色使用中,无法删除");
				return res;
			}
			
//			Role2MenuExample role2MenuExample = new Role2MenuExample();
//			role2MenuExample.createCriteria().andRoleidIn(longIds);
//			List<ParkRole2menu> rmList = role2MenuService.selectByExample(role2MenuExample);
			Wrapper<BfRole2menu> wrapper2=new EntityWrapper<BfRole2menu>();
			wrapper2.in("roleid", longIds);
			List<BfRole2menu> rmList = role2MenuService.selectList(wrapper2);

			this.saveUserlog(request, "角色管理："+userSession.getUsername()+"删除角色操作");
			
			if(!rmList.isEmpty()||rmList.size()>0){
				res.put("success", "false");
				res.put("msg","角色使用中,无法删除");
				return res;
			}
			
			
			/**
			 * 继续判断其他关联关系....
			 */
			roleService.deleteBatchIds(longIds);
			
			
			
			res.put("success", "true");
		} catch (Exception e) {
			res.put("success", "false");
			res.put("msg", e.getMessage());
		}
		return res;
	}

}
